﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
    /// <summary>
    /// 查询表达式是在编译时校验的
    /// </summary>
    public class demo_ling
    {
        /// <summary>
        /// 用LING来查询泛型集合用
        /// </summary>
        private class Car
        {
            public string PetName { get; set; }
            public string Color { get; set; }
            public int Speed { get; set; }
            public string Make { get; set; }

            public override string ToString()
            {
                return string.Format("Make = {0},Color = {1},Speed = {2},PetName = {3}",
                    Make, Color, Speed, PetName);
            }
        }

        private string[] currentVideoGames = {"Morrowind","Dead Rising",
                                                 "Half Life 2: Episode 1",
                                                 "F.E.A.R",
                                                 "Daxter",
                                                 "System Shock 2"
                                             };

        //使用对象初始化器构造一系列CAR对象
        private List<Car> myCars = new List<Car>(){
            new Car{ PetName = "Henry",Color = "Sliver",Speed = 400, Make = "BMW"},
            new Car{ PetName = "Daisy",Color = "Tan",Speed = 200, Make = "Yugo"},
            new Car{ PetName = "Wangx",Color = "Black",Speed = 150, Make = "Aodi"},
            new Car{ PetName = "Mary",Color = "White",Speed = 180, Make = "Benz"},
            new Car{ PetName = "liut",Color = "Red",Speed = 320, Make = "Toyata"},
            new Car{ PetName = "liut",Color = "Red"}
        };

        public void GetStrings()
        {
            var a = myCars.Find(delegate(Car car) { return car.Speed > 50; });

            var b = myCars.FindAll(delegate(Car car) { return car.Speed > 50; });

            var c = myCars.Find(car => car.Speed > 50);


            // 建造一个查询表达式，来代表数组中多于6个字母的选项
            IEnumerable<string> Subset = from g in currentVideoGames
                                         where g.Length > 6
                                         orderby g
                                         select g;
            foreach (string s in Subset)
            {
                Console.WriteLine("Item: {0}", s);
            }

            Console.WriteLine("--------------------");

            // 采用VAR
            var subset = from g in currentVideoGames
                         where g.Length > 6
                         orderby g
                         select g;
            foreach (var i in subset)
            {
                Console.WriteLine("Item: {0}", i);
            }
        }

        /// <summary>
        /// 使用的是LING查询泛型集合
        /// </summary>
        public void GetCars()
        {
            var fastCars = from c in myCars
                           where c.Speed > 200 && c.Make == "BMW"
                           select c;
            foreach (var car in fastCars)
            {
                Console.WriteLine("{0} is going too fast!", car.ToString());
            }

            Console.WriteLine("-------------------------------------------------------------------------");

            //下面采用另一种对象初始化器构造，此代码表示成了高度
            //函数式的语法的形式
            var myAntherCars = new[] {
                new { PetName = "Henry",Color = "Sliver",Speed = 400, Make = "BMW"},
                new { PetName = "Daisy",Color = "Tan",Speed = 200, Make = "Yugo"},
                new { PetName = "Wangx",Color = "Black",Speed = 150, Make = "Aodi"},
                new { PetName = "Mary",Color = "White",Speed = 180, Make = "Benz"},
                new { PetName = "liut",Color = "Red",Speed = 320, Make = "Benz"},
            };
            var fastAntherCars = from c in myAntherCars
                                 where c.Speed > 200 && c.Make == "BMW"
                                 select c;
            foreach (var car in fastAntherCars)
            {
                Console.WriteLine("{0} is going too fast!", car);
            }

            Console.WriteLine("-------------------------------------------------------------------------");
            var allCars = from c in myCars
                          where c.Speed > 200
                          select c.PetName;
            foreach (var n in allCars)
            {
                Console.WriteLine("Name : {0}", n);
            }


            Console.WriteLine("-------------------------------------------------------------------------");
            var carone = from c in myAntherCars
                         select c.Make;
            //过滤相同的值
            foreach (var n in carone.Distinct<string>())
            {
                Console.WriteLine("Name : {0}", n);
            }
        }

        public void GetArray()
        {
            //非泛型类型可包含任何类型的项，因为这些容器类的成员的原型是OBJECTS，
            //使用扩展方法OfType<T>可以过滤掉那些我们不想用的元素
            ArrayList myStuf = new ArrayList();
            myStuf.AddRange(new object[] { 10, 400, 8, false, new Car(), "string data" });
            //过滤出整型数据，并保存到myInts中
            var myInts = myStuf.OfType<int>();
            foreach (var i in myInts)
            {
                Console.WriteLine("Int value : {0}", i);
            }
        }

        public void QueryStringsWithSequenceAndLambads()
        {
            Console.WriteLine("****** Using Sequence/Lambda Expressions ******");

            string[] currentVideoGames = {"Morrowind","Dead Rising",
                                             "Half Life 2: Episode 1",
                                             "F.E.A.R","Daxter",
                                             "System Shock 2"};
            //通过Sequence类型赋予Array的扩展方法建立查询表达式
            var subset = currentVideoGames.Where(game => game.Length > 6)
                .OrderBy(game => game).Select(game => game);
            foreach (var game in subset)
            {
                Console.WriteLine("Item : {0}",game);
            }

            Console.WriteLine("****************** Using Ling ******************");
            var lingSub = from sub in currentVideoGames
                          where sub.Length > 6
                          orderby sub
                          select sub;
            foreach (var game in lingSub)
            {
                Console.WriteLine("Item : {0}", game);
            }
        }
    }
}
